$(function () {
		$(".group6 .tabs a").on("click", changetabs);
		$(".siderBar li").on("click", manuechange);
		$("#imgupload").on("click", upLoadImg);
		$(".tab1_sub").on("click", updataReader);
		$(".group3 .tabs a").on("click", changeRecordList);
		$("#tabTarget3 input:button").on("click", changePwd);
})

function updataReader() {
		var nick_name = $("#nick_name").val();
		var birthday = $("#birthday").val();
		var sex = $('input[name="sex"]:checked').val();
		var intro = $("#introtext").val();
		var address = $("#addresstext").val();

		$(".erro").empty();
		$.post("/wfRead/reader/updatareader",
				"nick_name=" + nick_name + "&sex=" + sex + "&birthday=" + birthday + "&intro=" + intro + "&address=" + address,
				function (data) {
						var msg = data.fieldErrers;
						if (data.flag) {
								alert("更新成功！");
						} else {
								//console.log(msg)
								if (msg.nickName != "") {
										//console.log(msg.nick_name);
										$("#nick_nameerror").text(msg.nickName);
								} else {
										$("#nick_nameerror").text("");
								}
								if (msg.birthday != "") {
										//console.log(msg.birthday);
										$("#birthdayerror").text(msg.birthday);
								} else {
										$("#birthdayerror").text("");
								}
								if (msg.intro != "") {
										//console.log(msg.intro);
										$("#introerror").text(msg.intro);
								} else {
										$("#introerror").text("");
								}
								if (msg.address != "") {
										//console.log(msg.address);
										$("#addresserror").text(msg.address);
								} else {
										$("#addresserror").text("");
								}
						}
				},
				"json");
}

function changetabs() {
		//var nowtab = $(" .group6 .checked").text()
		var nowtab = $(this).text()
		if ("基本设置" == nowtab) {
				$("#tabTarget2").css("display", "none");
				$("#tabTarget1").css("display", "block");
				$("#tabTarget3").css("display", "none");
				$(" .group6 .checked").removeClass("checked");
				$("div.group6 > div > div.tabs > a:nth-child(1)").addClass("checked");
		} else if ("头像设置" == nowtab) {
				$("#tabTarget1").css("display", "none");
				$("#tabTarget2").css("display", "block");
				$("#tabTarget3").css("display", "none");
				$(" .group6 .checked").removeClass("checked");
				$("div.group6 > div > div.tabs > a:nth-child(2)").addClass("checked");
		} else if ("修改密码" == nowtab) {
				$("#tabTarget1").css("display", "none");
				$("#tabTarget2").css("display", "none");
				$("#tabTarget3").css("display", "block");
				$(" .group6 .checked").removeClass("checked");
				$("div.group6 > div > div.tabs > a:nth-child(3)").addClass("checked");
		}
}

function manuechange() {

		var cl = $(this).attr("class");
		var groupid;
		if (cl == "") {
				var act = $(".siderBar .active");
				groupid = getGroupId(act)
				act.removeClass("active");
				$(".group" + groupid).css("display", "none");

				$(this).addClass("active");
				var id = getGroupId($(this));

				$(".group" + id).css("display", "block");
				if (id == 2 || id == 3 || id == 4) {
						getPage(1, 10);
				}
		}

}

function getGroupId(obj) {
		if (obj.text() == "首页") {
				return 1;
		} else if (obj.text() == "阅读记录") {
				return 2;
		} else if (obj.text() == "账务中心") {
				return 3;
		} else if (obj.text() == "我的书评") {
				return 4;
		} else if (obj.text() == "会员中心") {
				return 5;
		} else if (obj.text() == "个人资料") {
				return 6;
		}
}

//上传图片
function upLoadImg() {
		var fileObj = $(".fileup")[0].files[0]; // js 获取文件对象
		//console.log(fileObj);
		if (typeof (fileObj) == "undefined" || fileObj.size <= 0) {
				alert("请选择上传的图片！");
				return;
		} else if (fileObj.size > 1024 * 10240) {
				alert("上传文件大小不超过10MB!");
		}
		var formFile = new FormData();
		formFile.append("file", fileObj);
		var data = formFile;

		$.ajax({
				url: '/wfRead/reader/imgupload',
				type: 'post',
				dataType: 'json',
				data: data,
				cache: false,   //上传文件无需缓存
				processData: false,   // 用于对参数进行序列化处理，这里必须设为false
				contentType: false, // 必须
				success: function (data) {
						var urls = data.data;
						alert(data.message);
						if (data.flag) {
								urls[0]=urls[0].replace("static","")
								$("#tabTarget2 img").attr("src", "/" + urls[0]);
								$(".headimage").attr("src", "/" + urls[0])
						}
				}
		});
}

//获取阅读记录
function getReadRecord(currentPage, pageSize) {
		$(".record_list tbody").empty();
		var username = $("#user_name").text();
		$.post("/wfRead/reader/userrecord",
				"username=" + username + "&currentPage=" + currentPage + "&pageSize=" + pageSize,
				function (data) {

						var list = data.list;
						$(".current_page").val("");
						$(".group2 .current_page").val(currentPage);
						$(".all_page").text("")
						$(".group2 .all_page").text(data.pages);
						for (var i = 0; i < data.size; i++) {
								//list[i] = list[i].replace("\\","");
								list[i] = JSON.parse(list[i]);
								//console.log("url="+list[i].url)
								var tr = '<tr>\n' +
										'                                <td>' + (i + 1) + '</td>\n' +
										'                                <td><a href="/wfRead/novel/book/' + list[i].novelname + '" target="_blank">' + list[i].novelname + '</a></td>\n' +
										'                                <td><a href="' + list[i].url + '" target="_blank">' + list[i].catalogname + '</a></td>\n' +
										'                                <td>' + list[i].readtime + '</td>\n' +
										'                            </tr>';
								$(".record_list tbody").append(tr);
						}
				}, "json");
}

//获取消费记录
function getConsumerRecord(currentPage, pageSize) {
		//清空页面分页栏数据，避免相互干扰
		$(".current_page").val("");
		$(".all_page").text("");
		$.post("/wfRead/reader/getconsumerrecord", "currentPage=" + currentPage + "&pageSize=" + pageSize,
				function (data) {
						//console.log(data.data);
						var pageInfo = data.data;
						var size = pageInfo.size < 10 ? pageInfo.size : 10;
						var list = pageInfo.list;
						$(".consumer_list .current_page").val(pageInfo.pageNum);
						$(".consumer_list .all_page").text(pageInfo.pages);
						$(".consumer_list table tbody").empty();
						var pref = (pageInfo.pageNum - 1) * 10
						for (let i = 0; i < size; i++) {
								var li = '<tr>\n' +
										'\t\t<td>' + (pref + i + 1) + '</td>\n' +
										'\t\t<td>' + list[i].username + '</td>\n' +
										'\t\t<td>' + list[i].amount + '</td>\n' +
										'\t\t<td>' + Format(list[i].payTime, "yyyy-MM-dd HH:mm") + '</td>\n' +
										'\t\t<td>' + list[i].payType + '</td>\n' +
										'\t\t<td>' + list[i].remark + '</td>\n' +
										'</tr>';
								$(".consumer_list table tbody").append(li);
						}
				}, "json")
}

//获取充值记录
function getRechargeRecord(currentPage, pageSize) {
		//清空页面分页栏数据，避免相互干扰
		$(".current_page").val("");
		$(".all_page").text("");
		$.post("/wfRead/reader/getrechargerecord", "currentPage=" + currentPage + "&pageSize=" + pageSize,
				function (data) {
						var pageInfo = data.data;
						var size = pageInfo.size < 10 ? pageInfo.size : 10;
						var list = pageInfo.list;
						$(".recharge_list .current_page").val(pageInfo.pageNum);
						$(".recharge_list .all_page").text(pageInfo.pages);
						$(".recharge_list table tbody").empty();
						var pref = (pageInfo.pageNum - 1) * 10
						for (let i = 0; i < size; i++) {
								var li = '<tr>\n' +
										'\t\t<td>' + (pref + i + 1) + '</td>\n' +
										'\t\t<td>' + list[i].username + '</td>\n' +
										'\t\t<td>' + list[i].amount + '</td>\n' +
										'\t\t<td>' + Format(list[i].rechargeTime, "yyyy-MM-dd HH:mm") + '</td>\n' +
										'\t\t<td>' + list[i].remark + '</td>\n' +
										'</tr>';
								$(".recharge_list table tbody").append(li);
						}
				}, "json")
}

//获取用户评论
function getReaderComments(currentPage, pageSize) {
		$.post("/wfRead/reader/getcomments",
				"currentPage="+currentPage+"&pageSize="+pageSize,
				function (data) {
						var pageInfo = data.data;
						var size = pageInfo.size < 10 ? pageInfo.size : 10;
						var list = pageInfo.list;
						$(".comment_list .current_page").val(pageInfo.pageNum);
						$(".comment_list .all_page").text(pageInfo.pages);
						$(".comment_list table tbody").empty();
						var pref = (pageInfo.pageNum - 1) * 10
						for (let i = 0; i < size; i++) {
								//console.log(new String(list[i].commentContext).substring(0,20))
								var comment =  list[i].commentContext>20?new String(list[i].commentContext).substring(0,20):list[i].commentContext;
								var li = '<tr>\n' +
										'\t\t<td>' + comment+ '</td>\n' +
										'\t\t<td>' + Format(list[i].commentTime, "yyyy-MM-dd HH:mm") + '</td>\n' +
										'\t\t<td><a href="' + list[i].commentUrl + '" target="_blank">'+list[i].commentTitle+'</a></td>\n' +
										'</tr>';
								$(".comment_list table tbody").append(li);
						}
				},"json")
}
//重写工具栏：加载一页小说记录
function getPage(currentPage, pageSize) {
		//console.log($(".group2").css("display"))
		if ($(".group2").css("display") == "block") {
				getReadRecord(currentPage, pageSize)
		} else if ($(".group3").css("display") == "block") {
				if ($(".consumer_list").css("display") == "block") {
						getConsumerRecord(currentPage, pageSize);
				} else if ($(".recharge_list").css("display") == "block") {
						getRechargeRecord(currentPage, pageSize);
				}
		}else if ($(".group4").css("display") == "block"){
				getReaderComments(currentPage, pageSize);
		}

}

function changeRecordList() {
		var tyep = $(this).text();
		if ("消费记录" == tyep) {
				$(".recharge_list").css("display", "none");
				$(".consumer_list").css("display", "block");
				$(" .group3 .checked").removeClass("checked");
				$("div.group3 > div > div.tabs > a:nth-child(1)").addClass("checked");
				//getConsumerRecord(1,10);
		} else if ("充值记录" == tyep) {
				$(".recharge_list").css("display", "block");
				$(".consumer_list").css("display", "none");
				$(" .group3 .checked").removeClass("checked");
				$("div.group3 > div > div.tabs > a:nth-child(2)").addClass("checked");
				//getRechargeRecord(1,10);
		}
		getPage(1, 10)
}

function changePwd() {
		var oldPwd = $("#oldpwd").val()
		var newPwd = $("#newpwd").val()
		var cfmPwd = $("#confirmpwd").val()
		$.post("/wfRead/reader/updatepassword",
				"newPassword="+newPwd+"&oldPassword="+oldPwd+"&cfmPassword="+cfmPwd,
				function (data) {
					if (data.flag){
							alert(data.message)
							$("#oldpwd").val("")
							$("#newpwd").val("")
							$("#confirmpwd").val("")
							$("#pwd_erro").empty()
					}else {
							$("#pwd_erro").text(data.message)
					}
				},"json")
}